<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\MchRefundModel;

/**
 * 微信商户退款订单
 * Class MchOrder
 * @package Controller
 */
class MchRefund extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 创建微信退款订单
     * @param int $_pay_order_id 支付商户订单id
     * @param double $_money 退款金额
     * @param int $_uid 用户id
     * @return bool|string
     */
    public function create($_pay_order_id,$_money,$_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        $_order_id = $this->createOrderId();
        $_data = array(
            "order_id" => $_order_id,
            "pay_order_id" => $_pay_order_id,
            "uniacid" => $this->_uniacid,
            "uid" => $_uid,
            "money" => $_money,
            "time" => TIMESTAMP
        );
        if(MchRefundModel::create($_data)) {
            return $_order_id;
        } else {
            return false;
        }
    }

    /**
     * 创建订单号id
     */
    private function createOrderId()
    {
        $_order_id = TIMESTAMP.mt_rand(10000,99999);
        if(MchRefundModel::where("order_id",$_order_id)->value("id")) {
            return $this->createOrderId();
        }
        return $_order_id;
    }


    /**
     * 修改数据
     * @param $_order_id
     * @param $_data
     * @return mixed
     */
    public function edit($_order_id,$_data)
    {
        return MchRefundModel::where("order_id",$_order_id)
            ->update($_data);
    }
}